/**
 * Created by CZ on 2017/4/6.
 */
module.exports = function ($scope,$rootScope,$interval,$state,RequestService,commonConfig,modelService,$localStorage,$filter  ) {
    $rootScope.DeveloperNavShow = false;  //隐藏nav
    $rootScope.DeveloperHeaderShow = true; //显示头部
    $rootScope.DeveloperFooterShow = true; //显示尾部
    //获取token
    var token = $localStorage.get('cloud').token;
    //getVerifyCode(); //获取验证码
    // $scope.img = commonConfig.$webroot+commonConfig.userApi+'user/user/getVerifyImage?access_token='+token;
    // $scope.getVerifyCode = function(){
    //     var time = new Date().getTime();
    //     $scope.img = commonConfig.$webroot+commonConfig.userApi+'user/user/getVerifyImage?'+time+'&access_token='+token;
    // };

    $scope.Developer = {
        userCode:'',
        email:'',
        EmailCode:'',
        password:'',
        rePassword:'',
        realName:'',
        mobile:'',
        identityNumber:'',
        qq:'',
        verifyCode:'',
        userTypeId:15, //开发者类型写死
        lastLoginIp:returnCitySN["cip"],
        lastLoginTime:$filter("date")(new Date(),'yyyy-MM-dd HH:mm:ss')
    };
    //检测用户名是否存在
    $scope.userCodeCheckResult = 0;
    $scope.checkUserCodeExists = function(){
        if($scope.Developer.userCode != undefined && $scope.Developer.userCode != '' ){
            RequestService.Request(commonConfig.userApi+'user/user/checkUserCodeExists?userCode='+$scope.Developer.userCode, 'GET').then(function(response){
                console.log(response);
                $scope.userCodeCheckResult = response.code;
            },function(error){
                console.log(error);
            });
        }
    };
    //检测邮箱是否存在
    $scope.EmailCheckResult = null;
    $scope.EmailCheckExists  = function(){   // 验证邮箱是否存在
        console.log($scope.Developer.email);
        if($scope.Developer.email != undefined && $scope.Developer.email != '' ){
            RequestService.Request(commonConfig.userApi+'user/user/checkEmailExists?email='+$scope.Developer.email+'&access_token='+token, 'GET').then(function(response){
                console.log(response);
                $scope.EmailCheckResult = response.code;
                switch($scope.EmailCheckResult){
                    case 0: //验证成功
                        $scope.SecondFlag = false; //打开获取邮箱验证码开关
                        break;
                    case 1: //邮箱格式不正确
                        $scope.EmailCheckMessage = '邮箱格式不正确！';
                        break;
                    case 100:  //邮箱在数据库中已存在
                        if(response.message != null){
                            $scope.EmailCheckMessage = response.message;
                        }else {
                            $scope.EmailCheckMessage = '邮箱已注册！';
                        }
                        break;
                }
            },function(error){
                console.log(error);
            });
        }
        //成功：0，邮箱在数据库中已存在：100，邮箱格式不正确：1
    };

    //获取邮箱验证码模块
    $scope.second = 120;
    $scope.SecondFlag = true;
    $scope.emailKey = null;
    $scope.getEmailCode = function(){  //获取邮箱验证码
        $scope.SecondFlag = true;
        //TODO 优化邮箱注册
        // RequestService.Request(commonConfig.userApi+'user/user/sendEmail?email='+$scope.Developer.email+'&access_token='+token, 'GET').then(function(response){
        //     console.log(response);
        //     if(response.code == 100){
        //         // modelService.openTipsModel('邮箱不存在！'); TODO:暂时隐藏邮箱超时的错误
        //         $scope.second = 120;
        //         $scope.SecondFlag = true;
        //         $interval.cancel(timer);
        //     }
        // },function(error){
        //     console.log(error);
        // });
        RequestService.Request(commonConfig.userApi+'user/verifyemail/getEmailKey?email='+$scope.Developer.email, 'POST').then(function(res){
            console.log(res);
            $scope.emailKey = res;
        },function(error){
            console.log(error);
        });
        var timer = $interval(function(){
            $scope.second -- ;
            if($scope.second == 0){
                $scope.SecondFlag = false;
                $interval.cancel(timer);
                $scope.second = 120;
            }
        },1000)
    };

    //验证邮箱验证码
    $scope.EmailCodeVerifyResult = 0;
    $scope.verifyEmailCode = function(){
        var verifyCode = {
            "key": $scope.emailKey,
            "verifyCode": $scope.Developer.EmailCode
        };
        RequestService.Request(commonConfig.userApi+'user/verifyemail/verifyCode', 'POST',verifyCode).then(function(res){
            console.log(res);
            $scope.EmailCodeVerifyResult = res.code;
        },function(error){
            console.log(error);
        });
    };
    //获取图片验证码
    $scope.getVerifyImage = function(){
        RequestService.Request(commonConfig.userApi+'user/verifyimage/getVerifyImage', 'GET').then(function(res){
            console.log(res);
            $scope.imgKey = res[0];
            $scope.img = res[1];
        },function(error){
            console.log(error);
        });
    };
    $scope.getVerifyImage();
    //验证图片验证码
    $scope.ImageCodeVerifyResult = 0;
    $scope.VerifyImageCode = function(){
        var data = {
            "key": $scope.imgKey,
            "verifyCode": $scope.Developer.verifyCode
        };
        RequestService.Request(commonConfig.userApi+'user/verifyimage/verifyCode', 'POST',data).then(function(res){
            console.log(res);
            $scope.ImageCodeVerifyResult = res.code;
        },function(error){
            console.log(error);
        });
    };
    //提交注册数据
    $scope.submitDeveloperForm = function(Developer){  //提交数据
        console.log(Developer);
        var data = {
            "company": "",
            "email": Developer.email,
            "id": 0,
            "identityNumber": Developer.identityNumber,
            "lastLoginIp": Developer.lastLoginIp,
            "lastLoginTime": Developer.lastLoginTime,
            "mobile": Developer.mobile.toString(),
            "openId": "",
            "qq": Developer.qq.toString(),
            "realName": Developer.realName,
            "tenantId": 0,
            "status": 0,
            "userCode": Developer.userCode,
            "userName": Developer.email,
            "userPassword": Developer.password,
            "userTypeId": Developer.userTypeId
        };
        RequestService.Request(commonConfig.userApi+'user/user?emailCode='+Developer.EmailCode+'&verifyCode='+Developer.verifyCode+'&access_token='+token, 'POST',data).then(function(response){
            console.log(response);
            //验证
            if(response.code == 0){
                $state.go('DeveloperIndex.RegisterResult');
            }else{
                modelService.openTipsModel('注册失败,'+response.message);
            }
        },function(error){
            console.log(error);
        });
    };
};